package com.test.mvcframework.pojo.handler;

import com.test.mvcframework.interceptor.HandlerInterceptor;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


/**
 * Handler执行链
 *
 * @author felix
 */
public class HandlerExecutionChain {
    private final Handler handler;

    private final List<HandlerInterceptor> interceptorList = new LinkedList<>();


    public HandlerExecutionChain(Handler handler, HandlerInterceptor... handlerInterceptors) {
        this.handler = handler;
        if (handlerInterceptors != null) {
            this.interceptorList.addAll(Arrays.asList(handlerInterceptors));
        }
    }

    /**
     * 添加拦截器
     *
     * @param handlerInterceptor 拦截器
     */
    public void addInterceptor(HandlerInterceptor handlerInterceptor) {
        this.interceptorList.add(handlerInterceptor);
    }

    /**
     * 获取handler
     *
     * @return handler
     */
    public Object getHandler() {
        return this.handler;
    }

    /**
     * 获取所有拦截器
     *
     * @return 所有拦截器
     */
    public List<HandlerInterceptor> getInterceptorList() {
        return interceptorList;
    }

    /**
     * 是否有拦截器
     *
     * @return true/false
     */
    public boolean hasInterceptor() {
        return this.interceptorList.size() > 0;
    }
}
